#import "WGView.h"

@implementation WGView  
   
- (id)initWithFrame:(struct CGRect)frame  
{  
	if ((self == [super initWithFrame:frame]) != nil)  
	{  
		float bgColor[] = {0.0, 0.0, 0.0, 1.0};	
		float bgColorLoading[] = {0.0, 0.0, 0.0, 1.0};	
		frame.origin.x = frame.origin.y = 0.0f;
		  
		[self setBackgroundColor:CGColorCreate(CGColorSpaceCreateDeviceRGB(), bgColor)];  
		
		// Init interface
		tView = [[UITransitionView alloc] initWithFrame:CGRectMake(frame.origin.x,
			45.0f, frame.size.width, frame.size.height)];
		vMap = [[Map alloc] initWithFrame:CGRectMake(frame.origin.x,
			0.0f, frame.size.width, frame.size.height - 45.0f)];
		vPrefs = [[UIView alloc] initWithFrame:CGRectMake(frame.origin.x,
			0.0f, frame.size.width, frame.size.height - 45.0f)];
		vLocation = [[Location alloc] initWithFrame:CGRectMake(frame.origin.x,
			0.0f, frame.size.width, frame.size.height - 45.0f)];
		vLoading = [[UITextView alloc] initWithFrame:CGRectMake(frame.origin.x,
			0.0f, frame.size.width, frame.size.height - 45.0f)];

		nbBot = [[UINavigationBar alloc] initWithFrame: CGRectMake(frame.origin.x, 
        		0.0f, frame.size.width, 45.0f)];
		nbBotItem = [[UINavigationItem alloc] initWithTitle: @"WG"];
		 							
		[nbBot setDelegate: self];
		[nbBot setTapDelegate: self];
  		[nbBot pushNavigationItem:nbBotItem]; 							
		[nbBot setBarStyle:5];
		[nbBot enableAnimation];		

		tPrefs = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 45.0f)];  

		[vLoading setHTML: @"<html><body style='background-color: #fff;'><img style='padding: 135px;' src=''/></body></html>"];
	
  		conf = [[Configuration alloc] initWithView:tPrefs];				

		[vPrefs addSubview:tPrefs];
		
		[self addSubview:tView];
		[self addSubview:nbBot];

		[self changeView:S_MAP];	

		[vMap setLoadingDelegate:self];
		[vMap init];
	}  
	return self;  
}

- (void) dealloc
{
	[conf dealloc];
	[conf release];
	
	[vLoading release];
	
	[tPrefs release];

	[vMap release];
	[vPrefs release];
	[vLocation release];
	
	[tView release];
	[nbBotItem release];
	[nbBot release];

	[super dealloc];
}

- (void) navigationBar:(UINavigationBar*)bar buttonClicked:(int)button
{
	switch(state) {
		case S_MAP: {
			switch(button) {
				case 1: {
					[self changeView:S_LOCATION];			
					break;
				}
				case 0:	{
					[self changeView:S_PREFS];			
					break;
				}
			}
			break;
		}
		case S_PREFS: {
			if(button == 1) {
				[conf save];
				[self changeView:S_MAP];
			}
			break;
		}
		case S_LOCATION: {
			if(button == 0) [self changeView:S_MAP];
			break;
		}
	}
}

- (void) changeView:(int)view
{
	switch (view) 
	{
		case S_MAP: {
			[nbBotItem setTitle:NSLocalizedString(@"Map", @"iMap")];
			[nbBot showLeftButton:NSLocalizedString(@"Location", @"iLocation") withStyle:0 rightButton:NSLocalizedString(@"Prefs", @"iPrefs") withStyle:3];
			
			if(state == S_PREFS)
				[tView transition:2 toView:vMap];
			else
				[tView transition:1 toView:vMap];				
			break;
		}
		case S_PREFS: {
			[nbBotItem setTitle:NSLocalizedString(@"Prefs", @"iPrefs")];
			[nbBot showLeftButton:NSLocalizedString(@"Map", @"iMap") withStyle:0 rightButton:nil withStyle:0];
			[tView transition:1 toView:vPrefs];
			break;
		}
		case S_LOCATION: {
			[nbBotItem initWithTitle:NSLocalizedString(@"Location", @"iLocation")];
			[nbBot showLeftButton:nil withStyle:0 rightButton:NSLocalizedString(@"Map", @"iMap") withStyle:0];
			[tView transition:2 toView:vLocation];
			break;
		}
	}

	state = view;
}
 
/*
	Callbacks
*/ 

- (void) startLoading
{
	[tView transition:6 toView:vLoading];

	NSLog(@"I know!");
}

- (void) stopLoading
{
	[tView transition:6 toView:vMap];

	NSLog(@"I know!!!");
}


 
@end  